package com.fr.design.plugin.sap;

import com.fr.data.impl.sap.SAPDatabaseConnection;
import com.fr.design.border.UITitledBorder;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.ipasswordfield.UIPassWordField;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.layout.TableLayout;
import com.fr.design.layout.TableLayoutHelper;
import com.fr.design.dialog.BasicPane;
import com.fr.general.Inter;

import javax.swing.*;
import java.awt.*;

public class SAPDefPane extends BasicPane {
	private UITextField ashostTextField;
	private UITextField sysnrTextField;
	private UITextField clientTextField;
	private UITextField userTextField;
	private JPasswordField passwdField;
	private UITextField langTextField;
	private UITextField poolCapacityTextField;
	private UITextField peakLimitTextField;

	public SAPDefPane() {
		this.setBorder(UITitledBorder.createBorderWithTitle("SAP" + ":"));
		this.setLayout(FRGUIPaneFactory.createLabelFlowLayout());
		JPanel innerthis = FRGUIPaneFactory.createY_AXISBoxInnerContainer_L_Pane();
		this.add(innerthis);

		JPanel nContentPane = FRGUIPaneFactory.createNormalFlowInnerContainer_M_Pane();
		innerthis.add(nContentPane);

		ashostTextField = new UITextField(15);
		sysnrTextField = new UITextField(15);
		clientTextField = new UITextField(15);
		userTextField = new UITextField(15);
		passwdField = new UIPassWordField(15);
		langTextField = new UITextField(15);
		poolCapacityTextField = new UITextField(15);
		peakLimitTextField = new UITextField(15);

		double f = TableLayout.FILL;
		double p = TableLayout.PREFERRED;
		double[] rowSize = {p, p, p, p, p};
		double[] columnSize = {p, f, p, f};
		Component[][] comps = {
				{new UILabel(Inter.getLocText("Host_Name") + ":"), ashostTextField, new UILabel(Inter.getLocText("SAP-System_Number") + ":"), sysnrTextField},
				{new UILabel(Inter.getLocText("Language") + ":"), langTextField, new UILabel(Inter.getLocText("Client") + ":"), clientTextField,},
				{new UILabel(Inter.getLocText("User") + ":"), userTextField, new UILabel(Inter.getLocText("Password") + ":"), passwdField},
				{new UILabel(Inter.getLocText("ConnectionPool_Attr")), null, null, null},
				{new UILabel(Inter.getLocText("Capacity") + ":"), poolCapacityTextField, new UILabel(Inter.getLocText("Max_Peak_Limit") + ":"), peakLimitTextField}
		};
		nContentPane.add(TableLayoutHelper.createCommonTableLayoutPane(comps, rowSize, columnSize, 4));
	}

	public void populate(SAPDatabaseConnection ob) {
		if (ob == null) {
			ob = new SAPDatabaseConnection();
		}
		this.ashostTextField.setText(ob.getAshost());
		this.sysnrTextField.setText(ob.getSysnr());
		this.clientTextField.setText(ob.getClient());
		this.userTextField.setText(ob.getUser());
		this.passwdField.setText(ob.getPasswd());
		this.langTextField.setText(ob.getLang());
		this.poolCapacityTextField.setText(ob.getPoolCapacity());
		this.peakLimitTextField.setText(ob.getPeakLimit());
	}

	public SAPDatabaseConnection update() {
		SAPDatabaseConnection SAPDatabase = new SAPDatabaseConnection();
		SAPDatabase.setAshost(this.ashostTextField.getText().trim());
		SAPDatabase.setSysnr(this.sysnrTextField.getText().trim());
		SAPDatabase.setClient(this.clientTextField.getText().trim());
		SAPDatabase.setUser(this.userTextField.getText().trim());
		SAPDatabase.setPasswd(this.passwdField.getText().trim());
		SAPDatabase.setLang(this.langTextField.getText().trim());
		SAPDatabase.setPoolCapacity(this.poolCapacityTextField.getText().trim());
		SAPDatabase.setPeakLimit(this.peakLimitTextField.getText().trim());
		return SAPDatabase;
	}

	protected String title4PopupWindow() {
		return "SAP";
	}
}
